local extension = Package:new("wq_beili")
extension.extensionName = "FkWqmt"
Fk:loadTranslationTable{
  ["wq_beili"] = "无期迷途·背离",
}

local Wqc = require "packages/FkWqmt/wq_common"
local U = require "packages/utility/utility"

-- 禁闭者：麦昆
local wq_maikun = General:new(extension, "wq_maikun", "qun", 3, 3, General.Female)
-- 技能：造像
local wq_skill_zaoxiang = fk.CreateTriggerSkill{
  name = "wq_skill_zaoxiang",
  anim_type = "special",
  events = {fk.GameStart, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart or (event == fk.EventPhaseStart and player.phase == Player.Start and player == target) then
      local room = player.room
      if not table.find(room:getAlivePlayers(), function(p) return #p:getPile("wq_pile_xiang") > 0 end) then
        return player:hasSkill(self)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = math.min(#room:getAlivePlayers(), 5)
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_zaoxiang_prompt:::"..num)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = math.min(#room:getAlivePlayers(), 5)
    local cids = room:getNCards(num)
    room:moveCardTo(cids, Card.Processing, nil, fk.ReasonJustMove, self.name)
    room:fillAG(player, cids)
    local cid = room:askForAG(player, cids, false, self.name)
    room:closeAG(player)
    if cid then
      local card = Fk:getCardById(cid)
      if room:getCardArea(card) == Card.Processing then
        player:addToPile("wq_pile_xiang", card, true, self.name)
      end
    end
    cids = table.filter(cids, function(id) return room:getCardArea(id) == Card.Processing end)
    if #cids > 0 then
      room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name)
    end
  end,
}
-- 技能：乱真
local wq_skill_luanzhen = fk.CreateActiveSkill{
  name = "wq_skill_luanzhen",
  anim_type = "control",
  card_num = 0,
  target_num = 2,
  prompt = function (self, selected, selected_cards)
    return "#wq_skill_luanzhen_prompt"
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected)
    return false
  end,
  target_filter = function(self, to_select, selected)
    local room = Fk:currentRoom()
    if #selected == 0 then
      return #room:getPlayerById(to_select):getPile("wq_pile_xiang") > 0
    else
      return true
    end
  end,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local luanzhen_skills = player:getMark("wq_skill_luanzhen_skillList")
    if luanzhen_skills then
      for _, skillName in ipairs(luanzhen_skills) do
        if player:hasSkill(skillName) then
          room:handleAddLoseSkills(player, "-"..skillName, nil)
        end
      end
      room:setPlayerMark(player, "wq_skill_luanzhen_skillList", {})
    end
    local target_from = room:getPlayerById(effect.tos[1])
    local target_to = room:getPlayerById(effect.tos[2])
    local cids = target_from:getPile("wq_pile_xiang")
    local dummy = Fk:cloneCard("dilu")
    dummy:addSubcards(cids)
    target_to:addToPile("wq_pile_xiang", dummy, true, self.name)
    if player == target_to then
      if not player:isKongcheng() then
        local cid = room:askForAG(player, cids, false, self.name)
        if cid then
          local card = Fk:getCardById(cid, true)
          local toGain = room:printCard(card.name, card.suit, card.number)
          room:moveCards({
            ids = {toGain.id},
            to = player.id,
            toArea = Card.PlayerHand,
            moveReason = fk.ReasonPrey,
            proposer = player.id,
            skillName = self.name,
            moveVisible = true,
          })
        end
      end
    else
      local luanzhen_skills = player:getMark("wq_skill_luanzhen_skillList")
      if luanzhen_skills then
        local ban_types = {Skill.Limited, Skill.Wake, Skill.Quest}
        for _, skill_name in ipairs(Fk.generals[target_to.general]:getSkillNameList()) do
          local skill = Fk.skills[skill_name]
          if (not (skill.lordSkill or table.contains(ban_types, skill.frequency))) and (not player:hasSkill(skill.name)) then
            room:handleAddLoseSkills(player, skill.name, nil)
            table.insertIfNeed(luanzhen_skills, skill.name)
          end
        end
        room:setPlayerMark(player, "wq_skill_luanzhen_skillList", luanzhen_skills)
      end
    end
  end,
}
local wq_skill_luanzhen_Buff = fk.CreateTriggerSkill{
  name = "#wq_skill_luanzhen_Buff",
  refresh_events = {fk.GameStart, fk.Damaged},
  can_refresh = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self)
    else
      return player:hasSkill(self) and player == target
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      room:setPlayerMark(player, "wq_skill_luanzhen_skillList", {})
    else
      local luanzhen_skills = player:getMark("wq_skill_luanzhen_skillList")
      if luanzhen_skills then
        for _, skillName in ipairs(luanzhen_skills) do
          if player:hasSkill(skillName) then
            room:handleAddLoseSkills(player, "-"..skillName, nil)
          end
        end
        room:setPlayerMark(player, "wq_skill_luanzhen_skillList", {})
      end
    end
  end,
}
wq_skill_luanzhen:addRelatedSkill(wq_skill_luanzhen_Buff)
-- 技能：摹拓
local wq_skill_mota = fk.CreateTriggerSkill{
  name = "wq_skill_mota",
  anim_type = "drawcard",
  events = {fk.Damage, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target and #target:getPile("wq_pile_xiang") > 0 and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      if event == fk.Damage then
        return true
      elseif event == fk.Damaged then
        return data.from
      end
    end 
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      return room:askForSkillInvoke(player, self.name, data, "#wq_skill_mota_prompt-damage::"..target.id)
    elseif event == fk.Damaged then
      return room:askForSkillInvoke(player, self.name, data, "#wq_skill_mota_prompt-damaged:"..data.from.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      room:drawCards(player, 1, self.name)
      room:drawCards(target, 1, self.name)
    else
      local from = data.from
      if from and (not from:isNude()) then
        local id = room:askForCardChosen(player, from, "he", self.name)
        room:throwCard(id, self.name, from, player)
      end
    end
  end,
}
-- 麦昆的技能
wq_maikun:addSkill(wq_skill_zaoxiang)
wq_maikun:addSkill(wq_skill_luanzhen)
wq_maikun:addSkill(wq_skill_mota)
-- 麦昆的翻译
Fk:loadTranslationTable{
  ["wq_maikun"] = "麦昆",
  ["#wq_maikun"] = "完美复刻",
  ["designer:wq_maikun"] = "妙霄",
  ["cv:wq_maikun"] = "官方",
  ["illustrator:wq_maikun"] = "官方",
  ["wq_skill_zaoxiang"] = "造像",
  [":wq_skill_zaoxiang"] = "游戏开始时或准备阶段，若场上没有「像」，你可以亮出牌堆顶X张牌，并将其中一张置于你的武将牌上，称为「像」（X为存活角色数且至多为5）。",
  ["$wq_skill_zaoxiang"] = "别眨眼，真假只差一瞬。",
  ["#wq_skill_zaoxiang_prompt"] = "造像：你可以亮出牌堆顶%arg张牌，并将其中一张置于你的武将牌上称为「像」",
  ["wq_skill_luanzhen"] = "乱真",
  [":wq_skill_luanzhen"] = "出牌阶段限一次，你可以移动场上的「像」。若你移动到的目标角色为其他角色，则你获得其武将牌上的所有技能（限定技、觉醒技、主公技除外）直到你受到伤害后或再次发动此技能，否则你复制一张「像」到手牌中。",
  ["$wq_skill_luanzhen"] = "以假乱真？这只是基础中的基础。",
  ["wq_pile_xiang"] = "像",
  ["#wq_skill_luanzhen_prompt"] = "乱真：你可以移动场上的「像」",
  ["wq_skill_mota"] = "摹拓",
  [":wq_skill_mota"] = "每回合限一次，有「像」的角色造成/受到伤害后，你可以与其分别摸一张牌/弃置伤害来源一张牌。",
  ["$wq_skill_mota"] = "你还愿意相信自己的眼睛吗？",
  ["#wq_skill_mota_prompt-damage"] = "摹拓：你可以与%dest分别摸一张牌",
  ["#wq_skill_mota_prompt-damaged"] = "摹拓：你可以弃置%src一张牌",
  ["~wq_maikun"] = "呵，真假生死……早就无所谓了……",
}

-- 禁闭者：唐
local wq_tang = General:new(extension, "wq_tang", "qun", 4, 4, General.Male)
-- 技能：电涌
local wq_skill_dianyong = fk.CreateTriggerSkill{
  name = "wq_skill_dianyong",
  anim_type = "drawcard",
  events ={fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and player.phase == Player.NotActive then
      local room = player.room
      local cards = {}
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            if card.name == "thunder__slash" then
              table.insert(cards, info.cardId)
            end
          end
        end
      end
      if #cards == 0 then
        return false
      end
      if #U.moveCardsHoldingAreaCheck(room, table.filter(cards, function (id) return room:getCardArea(id) == Card.DiscardPile end)) == 0 then
        return false
      end
      self.cost_data = cards
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_dianyong_prompt")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = table.simpleClone(self.cost_data)
    cards = U.moveCardsHoldingAreaCheck(room, table.filter(cards, function (id) return room:getCardArea(id) == Card.DiscardPile end))
    if #cards > 0 then
      room:moveCardTo(cards, Player.Hand, player, fk.ReasonPrey, self.name, nil, false, player.id)
    end
  end,
}
-- 技能：万钧
local wq_skill_wanjun = fk.CreateActiveSkill{
  name = "wq_skill_wanjun",
  anim_type = "offensive",
  min_card_num = 1,
  min_target_num = 1,
  prompt = "#wq_skill_wanjun_prompt",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isNude()
  end,
  card_filter = function(self, to_select, selected)
    return #selected < 3 and (not Self:prohibitDiscard(Fk:getCardById(to_select)))
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected < #selected_cards then
      local card = Fk:cloneCard("duel")
      card.skillName = self.name
      return card.skill:modTargetFilter(to_select, selected, Self.id, card)
    end
  end,
  feasible = function (self, selected, selected_cards)
    return #selected > 0 and #selected == #selected_cards
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:throwCard(effect.cards, self.name, player, player)
    local targets = table.map(effect.tos, function(id) return room:getPlayerById(id) end)
    for _, p in ipairs(targets) do
      room:setPlayerMark(p, "wq_skill_wanjun_target", 1)
    end
    room:useVirtualCard("duel", nil, player, targets, self.name)
  end,
}
local wq_skill_wanjun_buff = fk.CreateTriggerSkill{
  name = "#wq_skill_wanjun_buff",
  mute = true,
  refresh_events = {fk.DamageCaused, fk.EventPhaseEnd, fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    local room = player.room
    if player:hasSkill(self) then
      if event == fk.DamageCaused then
        return data.card and data.card.name == "duel" and data.card.skillName == "wq_skill_wanjun"
      elseif event == fk.AfterCardsMove then
        if player:hasSkill(self) and player.phase == Player.Play then
          local num = 0
          for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
              for _, info in ipairs(move.moveInfo) do
                local card = Fk:getCardById(info.cardId)
                if card.name == "thunder__slash" then
                  num = num + 1
                end
              end
            end
          end
          if num > 0 then
            self.cost_data = num
            return true
          end
        end
      elseif event == fk.EventPhaseEnd then
        return player == target and player.phase == Player.Play
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then
      data.damageType = fk.ThunderDamage
    elseif event == fk.AfterCardsMove then
      room:addPlayerMark(player, "wq_skill_wanjun_thunderSlash", self.cost_data)
    elseif event == fk.EventPhaseEnd then
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if p:isAlive() and p:getMark("wq_skill_wanjun_target") > 0 then
          if player:isAlive() then
            local num = player:getMark("wq_skill_wanjun_thunderSlash")
            room:addPlayerMark(p, "@wq_mark_gandian", num)
          end
          room:setPlayerMark(p, "wq_skill_wanjun_target", 0)
        end
      end
      if player:isAlive() then
        room:setPlayerMark(player, "wq_skill_wanjun_thunderSlash", 0)
      end
    end
  end,
}
wq_skill_wanjun:addRelatedSkill(wq_skill_wanjun_buff)
-- 技能：克己
local wq_skill_keji = fk.CreateTriggerSkill{
  name = "wq_skill_keji",
  events = {fk.DamageInflicted},
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.damageType == fk.ThunderDamage
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = math.min(data.damage, 2)
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_keji_prompt:"..target.id.."::"..num)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.damageType = fk.NormalDamage
    room:drawCards(player, math.min(data.damage, 2), self.name)
  end,
}
-- 唐的技能
wq_tang:addSkill(wq_skill_dianyong)
wq_tang:addSkill(wq_skill_wanjun)
wq_tang:addSkill(wq_skill_keji)
-- 唐的翻译
Fk:loadTranslationTable{
  ["wq_tang"] = "唐",
  ["#wq_tang"] = "风暴雷霆",
  ["designer:wq_tang"] = "妙霄",
  ["cv:wq_tang"] = "官方",
  ["illustrator:wq_tang"] = "官方",
  ["wq_skill_dianyong"] = "电涌",
  [":wq_skill_dianyong"] = "每名其他角色的回合限一次，当包含雷【杀】在内的牌进入弃牌堆后，你可以获得其中的所有雷【杀】。",
  ["$wq_skill_dianyong"] = "我这柄破枪，居然还有焕发第二春的机会……还真叫人感慨。",
  ["#wq_skill_dianyong_prompt"] = "电涌：你可以获得本次进入弃牌堆的牌中的所有雷【杀】",
  ["wq_skill_wanjun"] = "万钧",
  [":wq_skill_wanjun"] = "出牌阶段限一次，你可以弃置至多三张牌，视为使用一张以等量名其他角色为目标的【决斗】，此牌造成的伤害视为雷电伤害。此阶段结束时，你令这些角色分别获得X枚「感电」标记（X为此阶段内进入弃牌堆的雷【杀】数量）。"
  ..Wqc.getTranslateInNewLine("#wq_common_mark_gandian_description"),
  ["$wq_skill_wanjun"] = "风雷交加！",
  ["#wq_skill_wanjun_prompt"] = "万钧：你可以弃置至多三张牌，视为使用一张以等量名其他角色为目标的【决斗】，此牌造成的伤害视为雷电伤害<br>此阶段结束时，你令这些角色分别获得X枚「感电」标记（X为此阶段内进入弃牌堆的雷【杀】数量）",
  ["wq_skill_keji"] = "克己",
  [":wq_skill_keji"] = "一名角色受到雷电伤害时，你可以令此伤害视为无属性伤害，然后你摸X张牌（X为伤害点数且至多为2）。",
  ["$wq_skill_keji"] = "铁锈上复燃的火焰不为毁灭，为的是照亮前路。",
  ["#wq_skill_keji_prompt"] = "克己：你可以令%src本次受到的伤害视为无属性伤害，然后你摸%arg张牌",
  ["~wq_tang"] = "要死了吗……呵呵……也不是第一次了……",
}

-- 禁闭者：白逸
local wq_baiyi = General:new(extension, "wq_baiyi", "qun", 3, 3, General.Female)
-- 技能：风乱
local wq_skill_fengluan = fk.CreateTriggerSkill{
  name = "wq_skill_fengluan",
  anim_type = "control",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and player:usedSkillTimes(self.name, Player.HistoryPhase) < 3
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(table.filter(room:getOtherPlayers(player), function(p) return not p:isNude() end), Util.IdMapper), 1, 1, "#wq_skill_fengluan_prompt", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dest = room:getPlayerById(self.cost_data)
    if dest:isNude() then return end
    local cid = room:askForCardChosen(player, dest, "he", self.name)
    if cid then
      if room:getCardArea(cid) == Card.PlayerEquip then
        room:obtainCard(player, cid, true, fk.ReasonPrey, player.id)
      elseif room:getCardArea(cid) == Card.PlayerHand then
        room:throwCard(cid, self.name, dest, player)
      end
    end
  end,
}
-- 技能：待发
local wq_skill_daifa = fk.CreateTriggerSkill{
  name = "wq_skill_daifa",
  anim_type = "drawcard",
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("wq_skill_daifa_mark_damage") == 0 then
      return room:askForSkillInvoke(player, self.name, data, "#wq_skill_daifa_prompt")
    else
      room:setPlayerMark(player, "wq_skill_daifa_mark_damage", 0)
      return false
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:drawCards(player, 3, self.name)
  end,
  refresh_events = {fk.Damage},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "wq_skill_daifa_mark_damage", 1)
  end,
}
-- 技能：埋藏
local wq_skill_maicang = fk.CreateTriggerSkill{
  name = "wq_skill_maicang",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      for _, move in ipairs(data) do
        if move.from and move.from == player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = 0
    for _, move in ipairs(data) do
      if move.from and move.from == player.id and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            num = num + 1
          end
        end
      end
    end
    num = math.min(3, num)
    data.cost_data = num
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_maicang_prompt-invoke:::"..num)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = data.cost_data
    local choices = {}
    table.insert(choices, "#wq_skill_maicang_choice-self:::"..num)
    table.insert(choices, "#wq_skill_maicang_choice-all:::"..num)
    local choice = room:askForChoice(player, choices, self.name)
    if choice:startsWith("#wq_skill_maicang_choice-self") then
      room:drawCards(player, num, self.name)
    else
      local tids = room:askForChoosePlayers(player, table.map(room:getAlivePlayers(), Util.IdMapper), num, num, "#wq_skill_maicang_prompt-choose:::"..num, self.name, true)
      if #tids > 0 then
        table.forEach(tids, function(id)
          local p = room:getPlayerById(id)
          room:drawCards(p, 1, self.name)
        end)
      end
    end
  end,
}
-- 白逸的技能
wq_baiyi:addSkill(wq_skill_fengluan)
wq_baiyi:addSkill(wq_skill_daifa)
wq_baiyi:addSkill(wq_skill_maicang)
-- 白逸的翻译
Fk:loadTranslationTable{
  ["wq_baiyi"] = "白逸",
  ["#wq_baiyi"] = "瞬息超速",
  ["designer:wq_baiyi"] = "妙霄",
  ["cv:wq_baiyi"] = "官方",
  ["illustrator:wq_baiyi"] = "官方",
  ["wq_skill_fengluan"] = "风乱",
  [":wq_skill_fengluan"] = "出牌阶段限三次，当你使用牌时，你可以指定一名其他角色。你弃置其一张手牌，或获得其装备区里的一张牌。",
  ["$wq_skill_fengluan"] = "你们，逃不掉了！",
  ["#wq_skill_fengluan_prompt"] = "风乱：你可以指定一名其他角色，你弃置其一张手牌，或获得其装备区里的一张牌。",
  ["wq_skill_daifa"] = "待发",
  [":wq_skill_daifa"] = "出牌阶段结束时，若你于此阶段内未造成过伤害，你可以摸三张牌。",
  ["$wq_skill_daifa"] = "留得青山在~",
  ["#wq_skill_daifa_prompt"] = "待发：你可以摸3张牌",
  ["wq_skill_maicang"] = "埋藏",
  [":wq_skill_maicang"] = "当你的牌因弃置而进入弃牌堆时，你可以选择一项：1.摸X张牌；2.令X名角色分别摸一张牌（X为进入弃牌堆的牌数且至多为3）。",
  ["$wq_skill_maicang"] = "我再也不想失去任何人了。",
  ["#wq_skill_maicang_prompt-invoke"] = "埋藏：你可以选择一项：1.摸%arg张牌；2.令%arg名角色分别摸一张牌",
  ["#wq_skill_maicang_choice-self"] = "摸%arg张牌",
  ["#wq_skill_maicang_choice-all"] = "令%arg名角色分别摸一张牌",
  ["#wq_skill_maicang_prompt-choose"] = "埋藏：请选择%arg名角色分别摸一张牌",
  ["~wq_baiyi"] = "照顾好他们……拜托你了……",
}

return extension